import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant';

//   进度条
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

Vue.use(VueRouter)

const routes = [
  {
    path: '/myregister',
    name: 'myregister',
    meta: { title: '注册' },
    component: () => import('../components/MyRegister.vue')
  },
  {
    path: '/mysearch',
    name: 'mysearch',
    meta: { title: '搜索' },
    component: () => import('../components/MySearch.vue')
  },
  {
    path: '/myparticulars/:name',
    name: 'myparticulars',
    meta: { title: '详情' },
    component: () => import('../components/MyParticulars.vue')
  },
  {
    path: '/myaddress',
    name: 'myaddress',
    meta: { title: '收货地址' },
    component: () => import('../components/MyAddress.vue')
  },
  {
    path: '/pushaddress',
    name: 'pushaddress',
    meta: { title: '新增地址' },
    component: () => import('../components/PushAddress.vue')
  },
  {
    path: '/mycollection',
    name: 'mycollection',
    meta: { title: '我的收藏' },
    component: () => import('../components/MyCollection.vue')
  },
  {
    path: '/mylogin',
    name: 'mylogin',
    meta: { title: '登录' },
    component: () => import('../components/MyLogin.vue')
  },

  //!   404 路由
  {
    path: '*',
    name: 'notfoud',
    meta: { title: '访问路径不存在' },
    component: () => import('../components/NotFoud.vue')
  },
  {
    path: '/myindex',
    name: 'myindex',
    meta: { title: '首页' },
    component: () => import('../components/MyIndex.vue'),

    children: [
      {
        path: '/myindex/index',
        name: 'index',
        meta: { title: '首页' },
        component: () => import('../views/index/index.vue'),
      },
      {
        path: '/myindex/mydingdan',
        name: 'mydingdan',
        meta: { title: '订单' },
        component: () => import('../components/MyDingdan.vue')
      },
      {
        path: '/myindex/order',
        name: 'order',
        meta: { title: '订单' },
        component: () => import('../views/index/order.vue'),
      },
      {
        path: '/myindex/mine',
        name: 'mine',
        meta: { title: '我的' },
        component: () => import('../views/index/mine.vue'),
      },
      {
        path: '/myindex',
        redirect: '/myindex/index',
      },
    ]
  },
  {
    path: '/',
    redirect: '/myregister',
  },

]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, form, next) => {
  NProgress.start();

  if (to.path == '/myindex/mine') {
    if (localStorage.getItem('token')) {
      next();
    } else {
      Toast.fail('访问受限，请先登录！')
      next('/mylogin')
    }
  } else {
    next();
  }
  if (to.path == '/myindex/order') {
    if (localStorage.getItem('token')) {
      next();
    } else {
      Toast.fail('访问受限，请先登录！')
      next('/mylogin')
    }
  } else {
    next();
  }
  if (to.path == '/myindex/mydingdan') {
    if (localStorage.getItem('token')) {
      next();
    } else {
      Toast.fail('访问受限，请先登录！')
      next('/mylogin')
    }
  } else {
    next();
  }
})

router.afterEach((to, form) => {
  document.title = to.meta.title;
  NProgress.done();
})
export default router
